
package artistic;

/**
 *
 * @author Mikael Andersson
 * @author Rurik Högfeldt
 */

public abstract class Brush {

    public abstract void setSize(int width, int height);

    //An implementation of Bresenham's line algorithm
    public void drawLine
            (TiledCanvas canvas, int x1, int y1, int x2, int y2)
    {
        boolean mostlyVertical = Math.abs(y2-y1)>Math.abs(x2-x1);
        
        //Swap x and y to prepare for iteration
        if(mostlyVertical)
        {
            int swap;

            swap = x1;
            x1 = y1;
            y1 = swap;

            swap = x2;
            x2 = y2;
            y2 = swap;
        }

        //Sort by virtual x for iteration (may be actual y)
        if(x1>x2)
        {
            int swap;

            swap = x1;
            x1 = x2;
            x2 = swap;

            swap = y1;
            y1 = y2;
            y2 = swap;
        }

        int dx = (x2-x1);
        int dy = Math.abs(y2-y1);

        int yStep = y2-y1<0?-1:1;
        
        int error = 0;
        int y=y1;
        for(int x=x1; x<x2; ++x)
        {
            if(mostlyVertical)
                draw(canvas, y, x);
            else
                draw(canvas, x, y);

            error+=dy;
            if((error<<1)>=dx){
                y+=yStep;
                error-=dx;
            }
        }
    }

    public abstract void draw(TiledCanvas canvas, int x, int y);
}
